import java.util.Arrays;
import java.util.regex.Pattern;

public class Leetcode937 {
    public String[] reorderLogFiles(String[] logs) {
        Arrays.sort(logs, (o1, o2) -> {
            String[] o1Array = o1.split(" ", 2);
            String[] o2Array = o2.split(" ", 2);

            if (Character.isDigit(o1Array[1].charAt(0))) {
                if (Character.isDigit(o2Array[1].charAt(0))) {
                    return 0;
                } else {
                    return 1;
                }
            } else {
                if (Character.isDigit(o2Array[1].charAt(0))) {
                    return -1;
                } else {
                    if (o1Array[1].equals(o2Array[1])) {
                        return o1Array[0].compareToIgnoreCase(o2Array[0]);
                    }

                    return o1Array[1].compareToIgnoreCase(o2Array[1]);
                }
            }
        });

        return logs;
    }
}
